package com.Sdebruijn.curriculumvitae.models;

import java.util.Collection;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * Subclass of {@link Experience} representing my educational experience
 * 
 * @author Stefan de Bruijn
 */
@DatabaseTable(tableName = "Education")
public class Education extends Experience {

	Education() {
		// all persisted classes must define a no-arg constructor with at least
		// package visibility
	}

	/**
	 * {@link Boolean} indicating whether the diploma belonging to this
	 * education has been received
	 */
	@DatabaseField
	@SerializedName("diploma")
	public boolean diploma;

	/**
	 * {@link String} indicating the name of the chosen minor in higher
	 * education
	 */
	@DatabaseField
	@SerializedName("minor")
	public String minor;

	/**
	 * {@link Collection} of voluntarily followed {@link Lesson}.
	 */
	@ForeignCollectionField
	@SerializedName("extra_lessons")
	public Collection<Lesson> extra_lessons; // Name Lesson chosen because Class
												// is excising interface

	/**
	 * {@link String} indicating the niveau of the education
	 */
	@DatabaseField
	@SerializedName("niveau")
	public String niveau;

	/*
	 * Required methods to have this class as Parcelable Having this class
	 * Parcelable will make it easier to restore it into fragments with
	 * savedInstanceState
	 */

	/*
	 * Just used as id - hashCode() will do fine
	 * 
	 * @see android.os.Parcelable#describeContents()
	 */
	@Override
	public int describeContents() {
		return this.hashCode();
	}

	/*
	 * Serialize my object attributes one by one
	 * 
	 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
	 */
	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeInt(this.id);
		parcel.writeString(this.end_date);
		parcel.writeString(this.minor);
		parcel.writeString(this.start_date);
		parcel.writeString(this.title);
		parcel.writeString(this.niveau);
		parcel.writeByte((byte) (this.current ? 1 : 0));
		parcel.writeByte((byte) (this.diploma ? 1 : 0));

		parcel.writeParcelable(this.institution, 0);
	}

	/**
	 * Required for any Parcelable class to be recreated. Will be called to
	 * de-seralize the object.
	 * 
	 * @author Stefan de Bruijn
	 * 
	 */
	public static final Parcelable.Creator<Education> CREATOR = new Parcelable.Creator<Education>() {
		public Education createFromParcel(Parcel source) {
			return new Education(source);
		}

		public Education[] newArray(int size) {
			return new Education[size];
		}
	};

	/**
	 * <b>Do not use!</b> Private constructor for the Parcelable Creator. Will
	 * de-serialize the object.
	 * 
	 * @param source
	 */
	private Education(Parcel source) {
		this.id = source.readInt();
		this.current = source.readByte() == 1;
		this.diploma = source.readByte() == 1;
		this.end_date = source.readString();
		this.minor = source.readString();
		this.start_date = source.readString();
		this.title = source.readString();
		this.niveau = source.readString();

		this.institution = source.readParcelable(Institution.class.getClassLoader());

	}
}
